﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Threading;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Sautom.ClientComunication.Commands;
using Sautom.ClientComunication.Events;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Authorization.ViewModels;

namespace Sautom.Modules.Authorization.Commands
{
    class ProccessLoginCommand : CommandBase
    {
        #region Properties
        private readonly IModuleManager _moduleManager;
        private readonly IModuleCatalog _moduleCatalog;
        private readonly AuthorizationService _authorizationService;
        private readonly EventAggregator _eventAggregator;
        private readonly LoginViewModel _viewModel;
        #endregion

        #region Constructors
        public ProccessLoginCommand(IModuleManager moduleManager, IModuleCatalog moduleCatalog, AuthorizationService authorizationService, EventAggregator eventAggregator, LoginViewModel viewModel)
        {
            _moduleManager = moduleManager;
            _moduleCatalog = moduleCatalog;
            _authorizationService = authorizationService;
            _eventAggregator = eventAggregator;
            _viewModel = viewModel;
        }
        #endregion

        #region Overrides of CommandBase

        public override void Execute()
        {
            var uiDispather = Dispatcher.CurrentDispatcher;
            (new Thread(() =>
                            {
                                bool isOk = _authorizationService.ProccessLogin(_viewModel.UserName, _viewModel.Password);
                                uiDispather.BeginInvoke(DispatcherPriority.Normal,
                                                        (Action)(() =>
                                                                     {
                                                                         if (isOk)
                                                                         {
                                                                             LoadModules();
                                                                         }
                                                                         else
                                                                         {
                                                                             ShowError();
                                                                         }
                                                                     }));
                            })).Start();
        }
        #endregion

        #region Private members
        private void LoadModules()
        {
            foreach (var module in _moduleCatalog.Modules)
            {
                _moduleManager.LoadModule(module.ModuleName);
            }

            _eventAggregator.GetEvent<AuthorizationSuccessEvent>().Publish("success");
        }

        private void ShowError()
        {
            _viewModel.HasError = true;
        }
        #endregion
    }
}
